package com.lei.goodsJsp.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lei.goodsJsp.entity.GoodsEntity;
import com.lei.goodsJsp.entity.PageEntity;

public class GoodsServlet extends HttpServlet {
	List<GoodsEntity> goodsList = new ArrayList();
	long count = 1L;

	@Override
	public void init() throws ServletException {
		System.out.println("servlet初始化....");
		goodsList.add(new GoodsEntity(count++, 001, "张三", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 002, "张二", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 003, "张四", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 012, "张五", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 044, "张六", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 004, "张七", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 005, "张八", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 006, "张九", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 007, "张十", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 057, "王五", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 032, "王六", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 024, "王七", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 056, "王八", "N", new Date(), new Date()));
		goodsList.add(new GoodsEntity(count++, 022, "王九", "N", new Date(), new Date()));
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		if ("add".equals(method)) {
			add(request);
			resirectList(response);
		} else if ("update".equals(method)) {
			update(request);
			resirectList(response);
		} else if ("toUpdate".equals(method)) {
			toUpdate(request, response);
		} else if ("delete".equals(method)) {
			delete(request);
			resirectList(response);
		} else if ("list".equals(method)) {
			list(request, response);
		} else {
			resirectList(response);
		}
	}

	private void resirectList(HttpServletResponse res) throws IOException {
		res.sendRedirect("GoodsServlet?method=list");
	}

	private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		PageEntity<GoodsEntity> page = page(req);
		req.setAttribute("myServlet", page);
		req.getRequestDispatcher("listJsp.jsp").forward(req, resp);
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
	}

	private void add(HttpServletRequest req) {
		String strShopNumber = req.getParameter("shopNumber");
//		Integer shopNumber = Optional.ofNullable(strShopNumber).map(e->Integer.valueOf(e)).orElse(null);
		Integer shopNumber = Optional.ofNullable(strShopNumber).map(Integer::valueOf).orElse(null);
		// Integer shopNumber=req.getParameter("shopNumber");
		String shopName = req.getParameter("shopName");
		String deleteFlag = req.getParameter("deleteFlag");
		String currenTtime = req.getParameter("currenTtime");
		String updateTime = req.getParameter("updateTime");

//		serviceOne.add(shopOneEntity);
		goodsList.add(new GoodsEntity(count++, shopNumber, shopName, deleteFlag, new Date(), new Date()));
	}

	private void delete(HttpServletRequest req) {
		Long id = Long.valueOf(req.getParameter("id"));
//		Long id = Optional.ofNullable(id).map(e -> Integer.parseInt(id)).orElse(1);
		Long Id = Optional.ofNullable(id).map(e -> Long.valueOf(e)).orElse(null);
		System.out.println(id);
		Iterator<GoodsEntity> iterator = goodsList.iterator();
		while (iterator.hasNext()) {
			GoodsEntity stu = iterator.next();
			if (stu.getId().equals(id)) {
				iterator.remove();
			}
		}
	}

	private void update(HttpServletRequest req) throws ServletException, IOException {
		System.out.println(req.getParameter("usere"));
		System.out.println("update");
//		String sho = req.getParameter("id");
//		Long id = Long.valueOf(sho);
		Long id = Long.valueOf(req.getParameter("id"));
//		Long id = Optional.ofNullable(id).map(e -> Integer.parseInt(id)).orElse(1);
		Long Id = Optional.ofNullable(id).map(e -> Long.valueOf(e)).orElse(null);

//		Integer shopNumber = req.getParameter("shopNumber");
//		shopNumber=Integer.parseInt("shopNumber");
//		Integer shopNumber = Optional.ofNullable(shopNumberOne).map(Integer::valueOf).orElse(null);
		String lei = req.getParameter("shopNumber");
		Integer shopNumber = Integer.valueOf(lei);
		String shopName = req.getParameter("shopName");
		Iterator<GoodsEntity> iterator = goodsList.iterator();
		while (iterator.hasNext()) {
			GoodsEntity shop = iterator.next();
			if (shop.getId().equals(id)) {
				shop.setShopName("shopNumber");
				shop.setShopName("shopName");

			}
		}
	}

	private void toUpdate(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String one = request.getParameter("id");
		Long id = Long.valueOf(one);
		GoodsEntity stu = null;
		Iterator<GoodsEntity> iterator = goodsList.iterator();
		while (iterator.hasNext()) {
			stu = iterator.next();
			if (stu.getId().equals(id)) {
				break;
			}
		}
		request.setAttribute("stu", stu);

		request.getRequestDispatcher("update.jsp").forward(request, response);
	}

	private PageEntity<GoodsEntity> page(ServletRequest req) {
		String currentPage = req.getParameter("currentPage");
		int intCurrentPage = Optional.ofNullable(currentPage).map(e -> Integer.parseInt(currentPage)).orElse(1);
		int pageSize = 3;
		int startIndex = (intCurrentPage - 1) * 3;
		int endIndex = intCurrentPage * 3;
		if (endIndex > goodsList.size()) {
			endIndex = goodsList.size();
		}
		List<GoodsEntity> newCardList = goodsList.subList(startIndex, endIndex);
		PageEntity<GoodsEntity> page = new PageEntity<GoodsEntity>();
		int totalPage = goodsList.size() % pageSize == 0 ? goodsList.size() / pageSize
				: goodsList.size() / pageSize + 1;
		page.setTotalPage(totalPage);
		page.setData(newCardList);
		page.setCurrentPage(intCurrentPage);
		page.setCount(goodsList.size());
		return page;
	}

}
